<?php
/**
 * 文章模型
 */
class Thread extends CI_Model {
    
    public function __construct() {
        parent::__construct();
    }
    
    /**
     * 增加文章 
     */
    public function addThread($title, $cid, $author, $content, $link, $datetime) {
        $data = array('title'=>$title, 'cid'=>$cid, 'author'=>$author, 'content'=>$content, 'link'=>$link, 'datetime'=>$datetime);
        return $this->db->insert('thread',$data);
    }
    
    /**
     * 修改文章
     */
    public function updateThread($tid, $data) {
        $this->db->where('tid',$tid);
        $res = $this->db->update('thread',$data);
        return $res;
    }
    
    /**
     * 删除文章
     */
    public function delThread($tid) {
        $data = array('tid'=>$tid);
        return $this->db->delete('thread',$data);
    }
    
    /**
     * 获取文章
     */
    public function getThread($condition, $offset, $pagesize, $row = TRUE) {
       $res = $this->db->get_where('thread',$condition,$pagesize,$offset);
       
       if($row) {
           return $res->row_array();
       }
       
       return $res->result_array();
    }
    
    /**
     * 获取文章列表
     */
    public function getThreadList($condition, $offset, $pagesize) {
        $this->db->select('tid, title, link, datetime');
        $this->db->order_by('datetime','desc');
        $res = $this->db->get_where('thread',$condition,$pagesize,$offset);
        return $res->result_array();
    }
    
    /**
     * 获取新闻总数
     */
    public function countAll($cid = '') {
        if($cid) {
            $this->db->where('cid',$cid);
        }
        $res = $this->db->get('thread');
        return $res->num_rows();
    }
    
    /**
     * 增加文章点击数
     */
    public function updateClick($tid) {
        if(!$tid) {
            return false;
        }
        $sql = "update thread set click = click + 1 where tid = '{$tid}'";
        $res = $this->db->query($sql);
        return true;
    }
    
    /**
     * 热点信息
     */
    public function getHotList() {        
        $sql = "select tid,title,datetime from thread where cid = 1 or cid = 3 or cid = 5 order by datetime desc limit 0,5";
        $res = $this->db->query($sql);
        return $res->result_array();
    }
}